<?php
namespace Admin\Controller;
use Think\Controller;
class SysqqController extends CommonController {
	//第三方登录显示  
    public function index()
    {
    	$sql = 'select * from sys_set where id=2';
    	$sys_set = M()->query($sql);
    	$this->assign('sys_set',current($sys_set));

		$this->display();

    }

    //开启QQ登陆 重写配置文件
    public function set_qq()
    {
    	//print_r($_POST);exit;
    	//Array ( [close] => on [AppId] => a [Appkey] => aa [callback] => aaa )
		if($_POST){
			$close = I('post.close');
			if ($close == 'on') {
				//开启QQ
		        $sql = 'update sys_set set set_state=1 where id=2';
		        $qq_start = M()->execute($sql);
		        if (!$qq_start) {
		            $arr = array(
		                'error' =>0,
		                'info'=>"QQ开启失败"
		                );
		            echo json_encode($arr);
		            exit;
		        }

			    unset($_POST['close']);
          //授权列表
          $scope = array("get_user_info","add_share","list_album","add_album","upload_pic","add_topic","add_one_blog","add_weibo","check_page_fans","add_t","add_pic_t","del_t","get_repost_list","get_info","get_other_info","get_fanslist","get_idolist","add_idol","del_idol","get_tenpay_addr");

			    $_POST['scope'] = $scope;
			    $_POST['storageType'] = "file";
			    $_POST['host'] = "localhost";
			    $_POST['user'] = "root";
			    $_POST['password'] = "root";
			    $_POST['database'] = "test";
			    $_POST['scope'] = implode(",",$_POST['scope']);
			    //$_POST['errorReport'] = (boolean) $_POST['errorReport'];
			    $setting = "<?php die('forbidden'); ?>\n";
			    $setting .= json_encode($_POST);
			    $setting = str_replace("\/", "/",$setting);
			    $incFile = fopen("./ThinkPHP/Library/Vendor/QQConnect/API/comm/inc.php","w+") or die("请设置API\comm\inc.php的权限为777");
			    if(fwrite($incFile, $setting)){
			        fclose($incFile);
			        fclose(fopen("setted.inc", "w"));

					$arr = array(
						'error' => 1,
						'info' => '配置成功,如果已配置成功并发布到外网，请只保留API目录下文件，删除intall目录下和其他文件'
						);
					echo json_encode($arr);
			    }else{
					$arr = array(
						'error' => 0,
						'info' => '配置文件写入失败'
						);
					echo json_encode($arr);
			    }
			}else{
				$arr = array(
					'error' => 0,
					'info' => '检查开启按钮是否打开'
					);
				echo json_encode($arr);
			}
		    
		}
    }

    //QQ停用 交互
    public function qq_stop()
    {
        //print_r($_POST);exit;
      if (IS_POST) {
        $set_state = I('post.set_state');
        $sql = 'update sys_set set set_state=0 where id=2';
        $qq_stop = M()->execute($sql);
        if ($qq_stop) {
            $arr = array(
                'error' =>0,
                'info'=>"QQ停用成功",
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' =>1,
                'info'=>"QQ停用失败"
                );
            echo json_encode($arr);
        }

      }
        
    }
}